﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using CPPProject.CppModel;
using CPPProject.CppServices.Dto;
using Microsoft.EntityFrameworkCore;
using OplugAbpProject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace CPPProject.CppServices
{
    public class T_Biz_ItemConfigHeadAppService : AsyncCrudAppService<T_Biz_ItemConfigHead, T_Biz_ItemConfigHeadDto, Guid, PagedResultRequestDto, UpdateT_Biz_ItemConfigHeadDto, UpdateT_Biz_ItemConfigHeadDto>, IT_Biz_ItemConfigHeadAppService
    {
        private readonly IObjectMapper _objectMapper;
        public T_Biz_ItemConfigHeadAppService(IRepository<T_Biz_ItemConfigHead, Guid> repository, IObjectMapper objectMapper) : base(repository)
        {
            _objectMapper = objectMapper;
        }
        #region 获取资产配置信息
        /// <summary>
        /// 获取资产配置信息
        /// </summary>
        /// <param name="nCompanyID">公司ID</param>
        /// <param name="skipCount"></param>
        /// <param name="maxResultCount"></param>
        /// <param name="cName">资产类型</param>
        /// <param name="category">资产性质（0部件-1产品）</param>
        /// <returns></returns>
        public async Task<PagedResultDto<T_Biz_ItemConfigHeadDto>> GetList(string nCompanyID, int skipCount = 0, int maxResultCount = 10, string cName = "", string category = "")
        {
            PagedResultDto<T_Biz_ItemConfigHeadDto> resultDto = new PagedResultDto<T_Biz_ItemConfigHeadDto>();
            List<T_Biz_ItemConfigHead> lists = new List<T_Biz_ItemConfigHead>();
            Expression<Func<T_Biz_ItemConfigHead, bool>> expr = o => o.NCompanyID == nCompanyID;
            if (!string.IsNullOrEmpty(cName))
            {
                expr = expr.ExpressionAnd(o => o.CName == cName);
            }
            if (!string.IsNullOrEmpty(category))
            {
                expr = expr.ExpressionAnd(o => o.ICategory == category);
            }
            lists = await Repository.GetAll().Where(expr).ToListAsync();
            var dtos = lists.Skip(skipCount).Take(maxResultCount).ToList();
            resultDto.Items = _objectMapper.Map<List<T_Biz_ItemConfigHeadDto>>(dtos);
            resultDto.TotalCount = lists.Count;
            return resultDto;
        }
        #endregion

        #region 获取资产类型
        /// <summary>
        /// 获取资产类型
        /// </summary>
        /// <param name="nConfigID">配置ID</param>
        /// <returns></returns>
        public async Task<T_Biz_ItemConfigHeadDto> GetCname(string nConfigID)
        {
            var list = await Repository.GetAllListAsync(o => o.NConfigID == nConfigID);
            return _objectMapper.Map<T_Biz_ItemConfigHeadDto>(list);
        }
        #endregion
    }
}
